package com.pl.buisness.profile.service.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.pl.buisness.profile.service.controller.fallback.ProfileControllerFallback;
import com.pl.myshop.commons.dto.ResponseResult;
import com.pl.myshop.commons.dto.errorcode.ErrorCodeEnmu;
import com.pl.myshop.provider.api.UesrService;
import com.pl.myshop.provider.domain.UmsAdmin;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Auther: pl
 * @Date: 2020/3/26 08:48
 * @Description:
 */
@RestController
@RequestMapping(value = "profile")
public class ProfileController {

    @Reference(version = "1.0.0")
    private UesrService userService;

    /**
     * @MethodName: getUserProfile
     * @Description: TODO
     * @Param: [userName]
     * @Return: com.pl.myshop.commons.dto.ResponseResult<com.pl.myshop.provider.domain.UmsAdmin>
     * @Author: pl
     * @Date: 10:11
    **/
    @GetMapping(value ="getUserProfile/{userName}")
    @SentinelResource(value = "getUserProfileFallback",fallback = "getUserProfileFallback",fallbackClass = ProfileControllerFallback.class)
    public ResponseResult<UmsAdmin> getUserProfile(@PathVariable String userName){
        /*   if ("admin".equals(userName)){
            throw new IllegalArgumentException("熔断");
        }*/

        UmsAdmin user = userService.getUserByUserName(userName);
        if (null == user){
            return new ResponseResult<UmsAdmin>(ErrorCodeEnmu.SEARCH_ERROR.getErrorCode(),ErrorCodeEnmu.SEARCH_ERROR.getMessage());
        }
        return new ResponseResult<UmsAdmin>(ErrorCodeEnmu.SEARCH_SUCCESS.getErrorCode(),ErrorCodeEnmu.SEARCH_SUCCESS.getMessage(),user);
    }
}